//
//  JokeViewController.m
//  Days
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 dss. All rights reserved.
//

#import "JokeViewController.h"
#import "JokeView.h"
#import "JokeTableViewCell.h"
#import "DataHandle.h"
#import "Model.h"
#import "MJRefresh.h"
#import "MJChiBaoZiHeader.h"
#import "MBProgressHUD.h"
#import "DataHandel.h"


static int i = 1;
@interface JokeViewController ()<UITableViewDataSource,UITableViewDelegate>




@property(nonatomic,strong)JokeView *jokeView;

@property(nonatomic,strong)NSArray *array;




@end

@implementation JokeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.navigationItem.title = @"开心一笑";
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"海水.jpeg"]];
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:imageView];
    
    self.jokeView = [[JokeView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.jokeView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.jokeView];
    

    //设置代理
    self.jokeView.tableView.delegate = self;
    self.jokeView.tableView.dataSource = self;
    
    //注册xib
    [self.jokeView.tableView registerNib:[UINib nibWithNibName:@"JokeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"JokeTableViewCell"];
    
    
    //网络请求之前
    [MBProgressHUD showHUDAddedTo:self.jokeView.tableView animated:YES];
    //下拉刷新
    self.jokeView.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        

      //判断是否有网
        [self judegNetWithcount:i --];
        
        //结束刷新
        [self.jokeView.tableView.mj_header endRefreshing];
        
    }];
    //开始刷新
    [self.jokeView.tableView.mj_header beginRefreshing];
    
    
    //上拉加载
    self.jokeView.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //判断是都有网
        [self judegNetWithcount:i ++];
                //结束刷新
        [self.jokeView.tableView.mj_footer endRefreshing];
        
    }];
    
    //开始上拉加载
    [self.jokeView.tableView.mj_footer beginRefreshing];
    
}


#pragma mark -- 判断是否有网
-(void)judegNetWithcount:(int)count{
    if(![[DataHandel sharedHandel]connectedToNetwork ])
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"网络连接失败,请查看网络是否连接正常！" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        
    }else{
        
        [self getDataFromNetWorkWithcount:count];
    }
    
}


#pragma mark -- 网络请求方法
- (void)getDataFromNetWorkWithcount:(int)count{
    
    
    [[DataHandle sharedDataHandle]getDataFromNetwithStr:count block:^(id isFinished) {
        
        self.array = isFinished;
        
        [MBProgressHUD hideHUDForView:self.jokeView.tableView animated:YES];
        
        if (isFinished) {
            [self.jokeView.tableView reloadData];

        }
    }];
}


#pragma mark ---实现代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [DataHandle sharedDataHandle].DataArray.count;
   

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    JokeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JokeTableViewCell"];
    cell.backgroundColor = [UIColor clearColor];
    Model *model = self.array[indexPath.row];
    cell.updateLabel.text = model.updatetime;
    cell.contentLabel.text = model.content;
    
    //获取到cell.label.text的自适应高度
    CGFloat height = [JokeTableViewCell heightForLabelText:cell.contentLabel.text];
    //修改cell.label的高度
    CGRect rect = cell.contentLabel.frame;
    rect.size.height = height;
    cell.contentLabel.frame = rect;
    
    return cell;


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    Model *model = self.array[indexPath.row];
    CGFloat height = [JokeTableViewCell heightForLabelText:model.content];
    return height+50;
    

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [[DataHandel sharedHandel].iFlySpeechSynthesizer stopSpeaking];
    
    

}


@end
